package com.fms.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fms.sys.entity.BlogArticle;
import com.fms.sys.mapper.BlogArticleMapper;
import com.fms.sys.service.IBlogArticleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class BlogArticleServiceImpl extends ServiceImpl<BlogArticleMapper, BlogArticle> implements IBlogArticleService {

    @Override
    public Page<BlogArticle> getArticleList(Page<BlogArticle> page, String title, Long categoryId, Integer status) {
        LambdaQueryWrapper<BlogArticle> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(title)) {
            wrapper.like(BlogArticle::getTitle, title);
        }
        if (categoryId != null) {
            wrapper.eq(BlogArticle::getCategoryId, categoryId);
        }
        if (status != null) {
            wrapper.eq(BlogArticle::getStatus, status);
        }
        wrapper.orderByDesc(BlogArticle::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    public boolean addArticle(BlogArticle article) {
        return this.save(article);
    }

    @Override
    public boolean updateArticle(BlogArticle article) {
        return this.updateById(article);
    }

    @Override
    public boolean deleteArticle(Long id) {
        return this.removeById(id);
    }

    @Override
    public BlogArticle getArticleById(Long id) {
        return this.getById(id);
    }

    @Override
    public boolean updateStatus(Long id, Integer status) {
        BlogArticle article = new BlogArticle();
        article.setId(id);
        article.setStatus(status);
        return this.updateById(article);
    }

    @Override
    public boolean increaseViewCount(Long id) {
        BlogArticle article = this.getById(id);
        if (article != null) {
            article.setViewCount(article.getViewCount() + 1);
            return this.updateById(article);
        }
        return false;
    }

    @Override
    public boolean increaseLikeCount(Long id) {
        BlogArticle article = this.getById(id);
        if (article != null) {
            article.setLikeCount(article.getLikeCount() + 1);
            return this.updateById(article);
        }
        return false;
    }
}
